//
//  Ltq_ repeatImage.m
//  UI05_简单的小相册功能
//
//  Created by Hanlingyu on 16/7/21.
//  Copyright © 2016年 ltq. All rights reserved.
//

#import "LTQRepeatImage.h"
#define KWidth self.frame.size.width
#define KHeight self.frame.size.height

@interface LTQRepeatImage ()<UIScrollViewDelegate>


@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) NSArray *imageArray;
@property (nonatomic, strong) NSTimer *timer;

@end


@implementation LTQRepeatImage


- (instancetype)initWithFrame:(CGRect)frame imageArray:(NSArray *)imageArray havePageControl:(BOOL)havePageControl {

    self = [super initWithFrame:frame];
    if (self) {
        
        _imageArray = imageArray;
        [self createView];
        
        if (havePageControl == YES) {
            [self createPageControl];
        }

    }


    return self;

}


- (void)createView {

    
    self.scrollView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.scrollView.backgroundColor = [UIColor cyanColor];
    self.scrollView.contentSize = CGSizeMake((self.imageArray.count + 2) * KWidth, 0);
    self.scrollView.contentOffset = CGPointMake(KWidth, 0);
    self.scrollView.pagingEnabled = YES;
    self.scrollView.bounces = NO;
    self.scrollView.tag = 1;
    
    self.scrollView.delegate = self;
    [self addSubview:self.scrollView];

    
    
    for (NSInteger i = 1; i <= self.imageArray.count; i++) {
        
        UIScrollView *smallScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(i * KWidth, 0, KWidth, KHeight)];
        smallScrollView.delegate = self;
        smallScrollView.minimumZoomScale = 1;
        smallScrollView.maximumZoomScale = 2;
        [self.scrollView addSubview:smallScrollView];
        smallScrollView.tag = smallScrollView.contentOffset.x / KWidth;
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, KWidth, KHeight)];
        imageView.backgroundColor = [UIColor redColor];
        imageView.image = [UIImage imageNamed:self.imageArray[i - 1]];
        [smallScrollView addSubview:imageView];
    }
    
    
    UIScrollView *firstSmallScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0 * KWidth, 0, KWidth, KHeight)];
    firstSmallScrollView.delegate = self;
    firstSmallScrollView.minimumZoomScale = 1;
    firstSmallScrollView.maximumZoomScale = 2;
    firstSmallScrollView.tag = firstSmallScrollView.contentOffset.x / KWidth;
    [self.scrollView addSubview:firstSmallScrollView];
    
    UIImageView *firstImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, KWidth, KHeight)];
    firstImageView.backgroundColor = [UIColor redColor];
    firstImageView.image = [UIImage imageNamed:self.imageArray[self.imageArray.count - 1]];
    [firstSmallScrollView addSubview:firstImageView];
    
    
    UIScrollView *lastSmallScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake((self.imageArray.count + 1) * KWidth, 0, KWidth, KHeight)];
    lastSmallScrollView.delegate = self;
    lastSmallScrollView.minimumZoomScale = 1;
    lastSmallScrollView.maximumZoomScale = 2;
    lastSmallScrollView.tag = lastSmallScrollView.contentOffset.x / KWidth;
    [self.scrollView addSubview:lastSmallScrollView];
    
    UIImageView *lastImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, KWidth, KHeight)];
    lastImageView.backgroundColor = [UIColor redColor];
    lastImageView.image = [UIImage imageNamed:self.imageArray[0]];
    [lastSmallScrollView addSubview:lastImageView];

    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:self.timer target:self selector:@selector(changePic) userInfo:nil repeats:YES];

}


- (void)createPageControl {

    UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, KWidth / 3, 20)];
    pageControl.center = CGPointMake(KWidth / 2, KHeight / 7 * 6);
    pageControl.backgroundColor = [UIColor colorWithRed:0.627 green:0.642 blue:0.632 alpha:1.000];
    pageControl.layer.cornerRadius = 10;
    pageControl.numberOfPages = 4;
    pageControl.tag = 1100;
    [self addSubview:pageControl];
    [pageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventTouchUpInside];

}

- (void)changePage:(UIPageControl *)pageControl {
    UIScrollView *scrollView = [self viewWithTag:1];
    [scrollView setContentOffset:CGPointMake(pageControl.currentPage * KWidth + KWidth, 0) animated:YES];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    scrollView = [self viewWithTag:1];
    if (self.scrollView.contentOffset.x == KWidth * 5) {
        self.scrollView.contentOffset = CGPointMake(KWidth * 1, 0);
    }
    if (self.scrollView.contentOffset.x == 0) {
        self.scrollView.contentOffset = CGPointMake(KWidth * 4, 0);
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    UIPageControl *pageControl = [self viewWithTag:1100];
    NSInteger number = (scrollView.contentOffset.x - KWidth) / KWidth;
    NSLog(@"%ld", number);
    pageControl.currentPage = number;
    for (UIScrollView *tempScroll in self.scrollView.subviews) {
        if ([tempScroll isKindOfClass:[UIScrollView class]]) {
            tempScroll.zoomScale = 1.0;
        }
    }
    
    
    
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    
    return [scrollView.subviews firstObject];
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
